package cn.yshujia.domain.vo;

import cn.yshujia.enums.Info;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 响应对象
 */

@Data
@Component
public class ApiVO<T> implements Serializable {
	
	@Serial
	private static final long serialVersionUID = 1905122041950251207L;
	
	private int code;
	
	private String msg;
	
	private T data;
	
	
	public ApiVO() {
	}
	
	public ApiVO(int code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}
	
	public static <T> ApiVO<T> success(int code, String msg, T obj) {
		return new ApiVO<>(code, msg, obj);
	}
	
	public static <T> ApiVO<T> success(T data) {
		return new ApiVO<>(Info.SUCCESS.getCode(), Info.SUCCESS.getMessage(), data);
	}
	
	public static <T> ApiVO<T> info() {
		return new ApiVO<>(Info.NOT_FOUND.getCode(), Info.NOT_FOUND.getMessage(), null);
	}
	
	public static <T> ApiVO<T> info(int code, String msg) {
		return new ApiVO<>(code, msg, null);
	}
	
	public static <T> ApiVO<T> error() {
		return new ApiVO<>(Info.SERVER_ERROR.getCode(), Info.SERVER_ERROR.getMessage(), null);
	}
	
	public static <T> ApiVO<T> error(int code, String msg) {
		return new ApiVO<>(code, msg, null);
	}
	
	public String toString() {
		return "ApiVO {+ "+ this.code + " <---> " + this.msg + "}";
	}
}